(function (p) { /* Sai.Calendar */

    var Calendar = function ( container, data ) {

        this.container      = container;
        this.menu           = new Sai.Calendar.Menu(container.children(".menu"));
        this.grid           = new Sai.Calendar.Grid(container.children(".grid"));
        this.agenda         = new Sai.Calendar.Agenda(container.children(".agenda"));
        this.currentDate    = new Date();
        this.currentDate.setMonth(this.currentDate.getMonth() - 1);

        this.menu.addEventListener(Sai.Calendar.Menu.ON_TODAY, $.proxy(this.onMenuTodayButtonClick, this));
        this.menu.addEventListener(Sai.Calendar.Menu.ON_PREV_MONTH, $.proxy(this.onMenuPrevMonthButtonClick, this));
        this.menu.addEventListener(Sai.Calendar.Menu.ON_NEXT_MONTH, $.proxy(this.onMenuNextMonthButtonClick, this));
        this.menu.addEventListener(Sai.Calendar.Menu.ON_MONTH_SELECT, $.proxy(this.onMenuMonthSelect, this));
        this.menu.addEventListener(Sai.Calendar.Menu.ON_AGENDA_SELECT, $.proxy(this.onMenuAgendaSelect, this));
        
        this.setData( data );
        this.setCurrentDate( this.currentDate.getFullYear(), this.currentDate.getMonth() );
        
        this.showGrid();
        //this.showAgenda();
    }

    Calendar.fn = Calendar.prototype;


    Calendar.fn.setData = function ( data ) {
        
        //console.log("");
        //console.log("Calendar.fn.setData");
        //console.log(data);
        this.grid.setData( data.events );
        this.agenda.setData( data.events );
        
    }


    Calendar.fn.setCurrentDate = function ( year, month ) {

        //console.log("Calendar.fn.setCurrentDate - year:"+year+" - month:"+month);
        
        this.currentDate = new Date(year, month);
        
        this.menu.setCurrentDate( year, month );
        this.grid.setCurrentDate( year, month );
        this.agenda.setCurrentDate( year, month );

    }


    Calendar.fn.showGrid = function () {

        this.menu.selectMonth();
        this.grid.container.show();
        this.agenda.container.hide();

    }


    Calendar.fn.showAgenda = function () {

        this.menu.selectAgenda();
        this.grid.container.hide();
        this.agenda.container.show();

    }


    Calendar.fn.onMenuTodayButtonClick = function ( event ) {
        
        var year    = Calendar.TODAY.getFullYear();
        var month   = Calendar.TODAY.getMonth();
        
        this.setCurrentDate( year, month );
    }


    Calendar.fn.onMenuPrevMonthButtonClick = function ( event ) {
        
        var newDate = new Date(this.currentDate.getTime());
            newDate.setMonth(this.currentDate.getMonth() - 1);
        
        var year    = newDate.getFullYear();
        var month   = newDate.getMonth();
        
        this.setCurrentDate( year, month );

    }


    Calendar.fn.onMenuNextMonthButtonClick = function ( event ) {
        
        var newDate = new Date(this.currentDate.getTime());
            newDate.setMonth(this.currentDate.getMonth() + 1);
        
        var year    = newDate.getFullYear();
        var month   = newDate.getMonth();
        
        this.setCurrentDate( year, month );

    }


    Calendar.fn.onMenuMonthSelect = function ( event ) {

        this.showGrid();

    }


    Calendar.fn.onMenuAgendaSelect = function ( event ) {

        this.showAgenda();

    }
    
    Calendar.MONTHS         = ["Enero","Febrero","Marzo","Abril","Mayo","Junio", "Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];
    Calendar.MONTHS_SHORT   = ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"];
    Calendar.DAYS           = ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"];
    Calendar.DAYS_SHORT     = ["dom", "lun", "mar", "mie", "jue", "vie", "sab"];
    Calendar.DAYS_MIN       = ["do","lu","ma","mi","ju","vi","sa"];
    Calendar.TODAY          = new Date();

    p.Calendar = Calendar;

})(Sai);